# relation.py
#


class Relation(object):
    def __init__(self, objects, pairs):
        """__init__

        objects - a collection of objects that contain an id member
        paris - a list of pairs of ids
        """

        self.objects = objects
        self.pairs = pairs

    def get_by_id(self, _id):
        for o in self.objects:
            if o._id == _id:
                return o
        return None

    def get_ordered(self, _id):
        """Get all the objects that are in relation to the object
        given by id."""

        for (id1, id2) in self.pairs:
            if id1 == _id:
                yield self.get_by_id(id2)

    def get_unordered(self, _id):
        """Get all the objects that are in relation to the object
        given by id or that the object given by id is in relation to."""

        for (id1, id2) in self.pairs:
            if id1 == _id:
                yield self.get_by_id(id2)
            if id2 == _id:
                yield self.get_by_id(id1)


